﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ConcreteMappersAndObjects.DomainObjects;
using DataMappers;
using ExaminationTT.Control;

namespace ExaminationTT.View
{
    public partial class SeasonsExams : Form
    {
        private BindingSource _examSeasonsDataSource;
        private List<ExamSeason> _examSeasons;
        private HostApplication _host;
        public SeasonsExams(HostApplication host, ICollection<ExamSeason> examSeasons)
        {
            _host = host;
            _examSeasons = examSeasons.ToList();
            _examSeasons.Sort(delegate(ExamSeason es1, ExamSeason es2)
            {
                return es1.StartSeason.Subtract(es2.StartSeason).Days;
            });
            
            _examSeasonsDataSource = new BindingSource();
            _examSeasonsDataSource.DataSource = _examSeasons;
            InitializeComponent();
            _dgv_ExamSeasonManagement.AutoGenerateColumns = false;
            _dgv_ExamSeasonManagement.DataSource = _examSeasonsDataSource;
            
        }

        private void _dgv_ExamSeasonManagement_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                if (e.FormattedValue == string.Empty)
                {
                    MessageBox.Show("Descrição inválida", this.Text);
                    e.Cancel = true;
                }
            }
            //else if (e.ColumnIndex == 1)
            //{
            //    DateTime result;
            //    if (!DateTime.TryParse(e.FormattedValue.ToString(), out result))
            //    {
            //        MessageBox.Show("Formato inválido de data", this.Text);
            //        e.Cancel = true;
            //    }
            //}
            else
            {
                DateTime result;
                if (!DateTime.TryParse(e.FormattedValue.ToString(), out result))
                {
                    MessageBox.Show("Formato inválido de data", this.Text);
                    e.Cancel = true;
                }
            }
        }

        private void _tsBtn_AddSeason_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Today;
            foreach (var item in _examSeasons)
	        {
                if (item.EndSeason.CompareTo(dt) > 0)
                {
                    dt = item.EndSeason.AddDays(1);
                }
	        }
            ExamSeason newES = new ExamSeason("Descrição", dt, dt.AddDays(20));
            _examSeasonsDataSource.Add(newES);
        }

        private void _tsBtn_RemoveSeason_Click(object sender, EventArgs e)
        {
            ExamSeason selected = _dgv_ExamSeasonManagement.CurrentRow.DataBoundItem as ExamSeason;
            _examSeasonsDataSource.Remove(selected);
            selected.Remove();
        }

        private void SeasonsExams_Leave(object sender, EventArgs e)
        {
            _host.SaveAllState();
        }
    }
}
